﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiveManager
{
    public partial class Supplier : Form
    {
        protected DiveDatabaseEntities diveDatabase;
        protected BindingSource bindingSource;

        public string id_weitergabe;
        
        public Supplier()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
            bindingSource = new BindingSource();
        }

        private void newSupplier_Click(object sender, EventArgs e)
        {
            //Öffnen der Form zum anlegen eines Lieferanten
            //Rechte überprüfen
            AddSupplier addsup = new AddSupplier();
            addsup.Show();
        }

        private void SupplierList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Ausgewähltes Item in der DB nachschlagen und ausgeben
            // Add Bindings
            //txShowFirma
            //txShowKDNR
        }

        private void btDetail_Click(object sender, EventArgs e)
        {
            //In den TextBoxen angezeigter Lieferant an SupplierDetails Form
            //übergeben und dort den Lieferanten im Detail anzeigen,
            //wenn entsprechende Rechte vorhanden
            this.txID.Visible = true;
            id_weitergabe = txID.Text;
            this.txID.Visible = false;
            SupplierDetails supdet = new SupplierDetails();
            supdet.Weitergabe_Details = id_weitergabe;
            supdet.Show();
        }

        private void Supplier_Load(object sender, EventArgs e)
        {
            //Alle Firmennamen aus der DB auslesen und in SupplierList ausgeben
            LoadData();

            txShowFirma.DataBindings.Add(new Binding("Text", bindingSource, "Firma"));
            txShowKDNR.DataBindings.Add(new Binding("Text", bindingSource, "KDNR_Lieferant"));
            txID.DataBindings.Add(new Binding("Text", bindingSource, "ID"));
           
            
        }



        protected void LoadData()
        {
            IQueryable<Supplier_Main> dataQuery = (from a in diveDatabase.Supplier_Main
                                                       select a);

            // Bind Data to BindingSource
            bindingSource.DataSource = dataQuery;

            // Bind BindingSource to List
            SupplierList.DataSource = bindingSource;


            // Hide ID-Columns
            SupplierList.Columns["ID"].Visible = false;
            SupplierList.Columns["Firma"].Visible = true;
            SupplierList.Columns["KDNR_Lieferant"].Visible = false;


            // Set Column Headers
            SupplierList.Columns["ID"].HeaderText = "ID";
            SupplierList.Columns["Firma"].HeaderText = "Lieferant";


        }



        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
  
        
    }
}
